/**
 * Created by cuppi on 2017/7/31.
 */
let host = '139.196.138.67';
let user = 'root';
let password = '7l_X_0qYhP';
let database = 'test';
const TABLE_NAME = 'authentication_coupon';


class CouponDAO{
    static getDBConnection() {

        var mysql = require('mysql');
        var connection = mysql.createConnection({
            host: host,
            user: user,
            password: password,
            database: database
        });
        connection.connect();
        return connection;
    }

    static queryPromise(connection, query) {
        return new Promise((resolve, reject) => {
            connection.query(query, function (error, results, fields) {
                if (error) {
                    reject(error);
                    return;
                }
                resolve(results);
            });
        });
    }

    static hasCouponByUserId(userId){
        return new Promise((resolve, reject) => {
            this.getCouponBuUserId(userId).then(list => {
                resolve(list.length > 0);
            }, error => {
                reject(error);
            })
        })
    }

    static getCouponByUserId(userId) {
        let connection = this.getDBConnection();
        return new Promise((resolve, reject) => {
            this.queryPromise(connection, {
                sql: 'SELECT * FROM ?? where user_id = ?',
                values: [TABLE_NAME, userId]
            }).then(list => {
                connection.end();
                let couponList = list.map(coupon => {
                    let userId = coupon.user_id;
                    let userGroup = coupon.user_group;
                    let couponMoney = coupon.coupon_money;
                    let isJoined = coupon.is_joined;
                    let createTime = coupon.create_time;
                    let updateTime = coupon.update_time;
                    return {userId, userGroup, couponMoney, isJoined, createTime, updateTime};
                });
                resolve(couponList);
            }, error => {
                connection.end();
                console.log(error);
            });
        });
    }

    static getAllCoupon(userId) {
        let connection = this.getDBConnection();
        return new Promise((resolve, reject) => {
            this.queryPromise(connection, {
                sql: 'SELECT * FROM ??',
                values: [TABLE_NAME]
            }).then(list => {
                connection.end();
                let couponList = list.map(coupon => {
                    let userId = coupon.user_id;
                    let userGroup = coupon.user_group;
                    let couponMoney = coupon.coupon_money;
                    let isJoined = coupon.is_joined;
                    let createTime = coupon.create_time;
                    let updateTime = coupon.update_time;
                    return {userId, userGroup, couponMoney, isJoined, createTime, updateTime};
                });
                resolve(couponList);
            }, error => {
                connection.end();
                console.log(error);
            });
        });
    }

    static setCoupon(coupon){
        let connection = this.getDBConnection();
        return new Promise((resolve, reject) => {
            this.queryPromise(connection, {
                sql: 'insert into ?? (user_id, user_group, coupon_money) values (?, ?, ?) ON DUPLICATE KEY UPDATE user_group = ?, coupon_money = ?',
                values: [TABLE_NAME, coupon.userId, coupon.userGroup, coupon.couponMoney, coupon.userGroup, coupon.couponMoney]
            }).then(result => {
                connection.end();
                resolve(result);
            }, error => {
                connection.end();
                console.log(error);
            });
        });
    }

}

module.exports = CouponDAO;
